﻿#include "app_startup_view.h"
#include "../../res/res.h"

// 组件声明，必须有
app_startup_view_t app_startup_view;

// 样式声明，注意需要在使用结束后释放
static lv_style_t style;    // 全局样式

/**
 * @brief 样式初始化
*/
static void style_init() {
    lv_style_init(&style);
    lv_style_set_bg_color(&style, lv_color_black());
    lv_style_set_pad_top(&style, 10);
    lv_style_set_pad_bottom(&style, 10);
    lv_style_set_pad_left(&style, 10);
    lv_style_set_pad_right(&style, 10);
    lv_style_set_text_color(&style, lv_palette_main(LV_PALETTE_RED));
    lv_style_set_text_letter_space(&style, 0);
    lv_style_set_text_line_space(&style, 20);
    lv_style_set_text_decor(&style, LV_TEXT_DECOR_NONE);
    lv_style_set_text_font(&style, &lv_font_montserrat_24);
    lv_style_set_text_color(&style, lv_color_white());
}

/**
 * @brief 样式释放
*/
static void style_delete() {
    lv_style_reset(&style);
}

/**
 * @brief 创建界面
 * @param root 
*/
void app_startup_view_create(lv_obj_t* root)
{
    // 初始化动画管理
    app_startup_view.anim_timeline = lv_anim_timeline_create();

    style_init();

    if (root == NULL)
    {
        app_log_error("page root is null! please check memory.");
    }

    // 初始化背景
    lv_obj_set_style_bg_color(root, lv_theme_get_color_primary(NULL), 0);
    lv_obj_set_style_bg_opa(root, LV_OPA_100, 0);

    // 初始化底层框
    app_startup_view.root = lv_obj_create(root);
    lv_obj_remove_style_all(app_startup_view.root);
    lv_obj_clear_flag(app_startup_view.root, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(app_startup_view.root, 522, 115);
    lv_obj_set_style_border_color(app_startup_view.root, lv_color_hex(0xff931e), 0);
    lv_obj_set_style_border_side(app_startup_view.root, LV_BORDER_SIDE_BOTTOM, 0);
    lv_obj_set_style_border_width(app_startup_view.root, 10, 0);
    lv_obj_set_style_border_post(app_startup_view.root, true, 0);
    lv_obj_align(app_startup_view.root, LV_ALIGN_CENTER, 0, -10);

    // 初始化Logo
    app_startup_view.img_logo = lv_img_create(app_startup_view.root);
    lv_img_set_src(app_startup_view.img_logo, &logo_white_522x65);
    lv_obj_align(app_startup_view.img_logo, LV_ALIGN_TOP_MID, 0, 0);

    app_startup_view.title = lv_label_create(app_startup_view.root);
    lv_obj_add_style(app_startup_view.title, &style, 0);
    lv_label_set_text_fmt(app_startup_view.title, "Welcome to Lumatek professional lighting");
    lv_obj_align(app_startup_view.title, LV_ALIGN_BOTTOM_MID, 0, 5);

    //lv_obj_t* l = lv_label_create(root);
    //lv_obj_set_style_text_color(l, lv_color_hex(0xcccccc), 0);
    //lv_obj_set_style_text_font(l, &lv_font_montserrat_18, 0);
    //lv_obj_set_style_text_opa(l, LV_OPA_70, 0);
    //lv_obj_set_width(l, 850);
    //lv_label_set_text(l, "Professional Grow Lighting solutions for wholesale, indoor growing and large horticultural projects."
    //    "Our wide range of products is unbeaten regarding performance and reliability."
    //    "We are a premium brand offering the best return on investment solution on the market.");
    //lv_obj_align(l, LV_ALIGN_BOTTOM_MID, 0, -10);

    lv_anim_timeline_wrapper_t wrapper[] =
    {
        ANIM_DEF_1S(0, app_startup_view.root, width, 0, lv_obj_get_style_width(app_startup_view.root, 0)),
        ANIM_DEF(1000, app_startup_view.img_logo, y, lv_obj_get_style_height(app_startup_view.root, 0), lv_obj_get_y(app_startup_view.img_logo)),
        LV_ANIM_TIMELINE_WRAPPER_END
    };

    lv_anim_timeline_add_wrapper(app_startup_view.anim_timeline, wrapper);
}

/**
 * @brief 页面销毁，挂在root下的页面，都会自动销毁
 * 此处主要销毁样式
*/
void app_startup_view_delete() {
    style_delete();
    if (app_startup_view.anim_timeline != NULL) {
        lv_anim_timeline_del(app_startup_view.anim_timeline);
        app_startup_view.anim_timeline = NULL;
    }
}
